import 'dart:io';
import 'package:mason_logger/mason_logger.dart';
import 'package:yoyo_get_fast_mirror/i18n.dart';

final done = lightGreen.wrap(GetfastmirroCommand.done);
final Logger _logger = Logger();
RegExp urlReg = RegExp(
  r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:[\w\d]+|([^[:punct:]\s]|/)))',
);

///mirror 镜像源网址
void writeMirrorIntoFile(String mirror, String path) {
  final writting = lightYellow.wrap('${GetfastmirroCommand.writting} $path');
  final file = File(path);
  var fileContent = file.readAsStringSync();
  final url = urlReg.allMatches(fileContent);
  for (final item in url) {
    fileContent = fileContent.replaceAll(item.group(0) ?? '', mirror);
  }
  _logger.info(writting);
  if (!file.existsSync()) file.createSync(recursive: true);
  file.writeAsStringSync(fileContent);
  _logger.info(done);
}
